package com.chainyoung.component_tmperson.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.base.BaseActivity;
import com.chainyoung.common.bean.UserInfo;
import com.chainyoung.common.common_widget.TimeCount;
import com.chainyoung.common.constant.LibConstant;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.common.utils.CommonUtil;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.common.utils.ToastUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.R2;
import com.chainyoung.component_tmperson.di.component.DaggerPersonActivityComponent;
import com.chainyoung.component_tmperson.di.module.PersonActivityModule;
import com.chainyoung.component_tmperson.ui.mvp.contract.UpdatePwdContract;
import com.chainyoung.component_tmperson.ui.mvp.presenter.UpdatePwdPresenter;

import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UpdatePwdActivity extends BaseActivity<UpdatePwdPresenter> implements UpdatePwdContract.View {

    @BindView(R2.id.et_pwd)
    EditText etPwd;
    @BindView(R2.id.et_confirm_pwd)
    EditText etConfirmPwd;
    @BindView(R2.id.et_code)
    EditText etCode;
    @BindView(R2.id.tv_send_code)
    TextView tvSendCode;


    private TimeCount timeCount;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPersonActivityComponent.builder()
                .appComponent(appComponent)
                .personActivityModule(new PersonActivityModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        setCurrentTitle(R.string.tmperson_update_pwd);
        timeCount = new TimeCount(60000, 1000, tvSendCode);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.tmperson_activity_update_pwd;
    }

    @OnClick(R2.id.tv_send_code)
    void sendCode() {
        if (CommonUtil.isFastDoubleClick()) {
            return;
        }
        UserInfo userInfo = CommonUtil.getUserInfo();
        String accountNo = TextUtils.isEmpty(userInfo.getMobile()) ? userInfo.getEmail() : userInfo.getMobile();
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("account", accountNo);
        map.put("type", "update_password");
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        mPresenter.sendCode(map);
    }

    @OnClick(R2.id.tv_submit)
    void submit() {
        if (StringUtil.viewGetTextIsEmpty(etPwd)) {
            ToastUtil.showToast(etPwd.getHint().toString());
            return;
        }
        if (StringUtil.viewGetTextIsEmpty(etConfirmPwd)) {
            ToastUtil.showToast(etConfirmPwd.getHint().toString());
            return;
        }
        if (StringUtil.viewGetTextIsEmpty(etCode)) {
            ToastUtil.showToast(etCode.getHint().toString());
            return;
        }
        if (!StringUtil.isPwdValidate(StringUtil.viewGetText(etPwd))) {
            ToastUtil.showToast(getString(R.string.pls_input_correct_pwd));
            return;
        }
        if (!StringUtil.viewGetText(etPwd).equals(StringUtil.viewGetText(etConfirmPwd))) {
            ToastUtil.showToast("两次密码不一致");
            return;
        }

        TreeMap<String, Object> map = new TreeMap<>();
        map.put("token", SPUtil.getString(LibConstant.TOKEN));
        map.put("password", StringUtil.viewGetText(etPwd));
        map.put("password_confirm", StringUtil.viewGetText(etConfirmPwd));
        map.put("verify_code", StringUtil.viewGetText(etCode));
        mPresenter.updatePwd(map);
    }

    @Override
    public void updatePwdSuccess(String data) {
        ToastUtil.showToast(data);
        finish();
    }

    @Override
    public void onSendCodeSuccess(String object) {
        showMsg(getString(R.string.validate_code_sent));
        timeCount.start();
    }


    @Override
    protected void onResume() {
        super.onResume();

    }



    @Override
    protected void onDestroy() {
        if (timeCount != null) {
            timeCount.cancel();
        }
        super.onDestroy();
    }
}
